package com.uyayo.oms.cms.biz;

import com.uyayo.oms.cms.model.TemplateFile;
import com.uyayo.oms.system.util.PathUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hfj on 2017/8/20.
 */

@Component
public class TemplateFileBiz {


        private final String TEMPLATE_PATH = PathUtil.getRootClassPath()+ File.separator+"templates"+File.separator+"website";

        public List<TemplateFile> findAll() throws Exception {
            List<TemplateFile> result = new ArrayList<>();
            File mainFile = new File(TEMPLATE_PATH);
            if(!mainFile.exists()||mainFile.listFiles().length<1) {
                throw new Exception("请检查模板目录是否存在！");
            }
            /*只遍历三级级目录*/
            for(File file :mainFile.listFiles()){
                TemplateFile templateFile = new TemplateFile();
                templateFile.setFileName(file.getName());
                templateFile.setFilePath(file.getAbsolutePath());
                templateFile.setIsDirectory(file.isDirectory());
                if(file.isDirectory()){
                    File subFile = new File(file.getAbsolutePath());
                    List<TemplateFile> childList = new ArrayList<>();
                    for(File subfile :subFile.listFiles()) {
                        TemplateFile subTemplateFile = new TemplateFile();
                        subTemplateFile.setFileName(subfile.getName());
                        subTemplateFile.setFilePath(subfile.getAbsolutePath());
                        subTemplateFile.setIsDirectory(subfile.isDirectory());
                        if(subfile.isDirectory()){
                            List<TemplateFile> subChildList = new ArrayList<>();
                            File subChildFile = new File(subfile.getAbsolutePath());
                            if(!subChildFile.exists()||mainFile.listFiles().length<1) continue;
                            for(File subChildfile :subChildFile.listFiles()) {
                                TemplateFile subChildTemplateFile = new TemplateFile();
                                subChildTemplateFile.setFileName(subChildfile.getName());
                                subChildTemplateFile.setFilePath(subChildfile.getAbsolutePath());
                                subChildTemplateFile.setIsDirectory(subChildfile.isDirectory());
                                subChildList.add(subChildTemplateFile);
                            }
                            subTemplateFile.setChildList(subChildList);
                        }
                        childList.add(subTemplateFile);
                    }
                    templateFile.setChildList(childList);
                }
                result.add(templateFile);
            }
            return result;
        }

        public TemplateFile findByPath(String path) throws Exception {
            File file = new File(path);
            if(!file.exists()) throw new Exception("模板不存在请检查！");
            TemplateFile templateFile = new TemplateFile();
            templateFile.setFileName(file.getName());
            templateFile.setFilePath(file.getAbsolutePath());
            templateFile.setContent(this.readTemplateFileContent(file));
            return templateFile;
        }


        public static void main(String[]args) throws Exception {
            List<TemplateFile> list = new TemplateFileBiz().findAll();
            for (TemplateFile templateFile : list){
                System.out.println(templateFile.getFileName());
                if(templateFile.getIsDirectory()){
                    for (TemplateFile t : templateFile.getChildList()){
                        System.out.println(templateFile.getFileName()+"---->"+t.getFileName()+"["+t.getFilePath()+"]");
                        if(t.getIsDirectory()){
                            for (TemplateFile st : t.getChildList()){ System.out.println(t.getFileName()+"---------->"+ st.getFileName()+"["+ st.getFilePath()+"]");}
                        }
                    }
                }
            }
//      System.out.println(new TemplateFileServiceImpl().findByPath("E:\\work\\idea_app\\CMS\\CicadasCms\\CicadasCms\\target\\classes\\templates\\www\\blog\\about.html").getContent());
        }

        public  String readTemplateFileContent(File file) throws IOException {

                InputStream inputStream = new FileInputStream(file);
                byte[] buffer = new byte[1024];
                int len;
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                while((len = inputStream.read(buffer)) != -1) {
                    bos.write(buffer, 0, len);
                }
                bos.close();
                return new String(bos.toByteArray(), "utf-8");
        }

        @Async
        public  void writeTemplateFileContent(TemplateFile templateFile) throws IOException {
                OutputStream outputStream = new FileOutputStream(new File(templateFile.getFilePath()));
                OutputStreamWriter os = new OutputStreamWriter(outputStream, "utf-8");
                os.write(templateFile.getContent());
                os.flush();
                os.close();
        }


        public String delete() {
            return null;
        }
    }
